Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(solver) resolve a data race with ops counters #381

Merged
merged 1 commit into from
May 21, 2021
Merged

Conversation

hbagdi
Copy link
Member

@hbagdi hbagdi commented May 13, 2021

No description provided.

@hbagdi hbagdi marked this pull request as ready for review May 13, 2021 21:54
@hbagdi hbagdi requested a review from a team as a code owner May 13, 2021 21:54
@codecov-commenter
Copy link

codecov-commenter commented May 13, 2021

Codecov Report

Merging #381 (1861bbe) into main (729559e) will decrease coverage by 7.51%.
The diff coverage is 30.43%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #381      +/-   ##
==========================================
- Coverage   54.19%   46.68%   -7.52%     
==========================================
  Files          61       87      +26     
  Lines        4971     6568    +1597     
==========================================
+ Hits         2694     3066     +372     
- Misses       1978     3136    +1158     
- Partials      299      366      +67     
Impacted Files Coverage Δ
cmd/common.go 0.00% <0.00%> (ø)
cmd/common_konnect.go 16.79% <0.00%> (+1.57%) ⬆️
solver/solver.go 8.88% <46.66%> (ø)
cmd/validate.go 20.00% <0.00%> (-3.34%) ⬇️
cmd/root.go 52.73% <0.00%> (-3.23%) ⬇️
cmd/konnect_diff.go 75.00% <0.00%> (-2.78%) ⬇️
cmd/konnect_dump.go 25.37% <0.00%> (-1.72%) ⬇️
cmd/dump.go 24.79% <0.00%> (-1.59%) ⬇️
cmd/konnect_sync.go 70.00% <0.00%> (-1.43%) ⬇️
... and 39 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 729559e...1861bbe. Read the comment docs.

@hbagdi
Copy link
Member Author

hbagdi commented May 18, 2021

Had a conversation with @mflendrich on this and updated the code to use a mutex instead of atomic as that package has a ton of gotchas.

solver/solver.go Outdated
a.counter += delta
}

func (a *AtomicInt32Counter) Read() int32 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - Count might be a better name because it describes the returned value better, and because Read is already in use by io.Reader.

Comment on lines +15 to +20
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
a.Increment(int32(1))
}()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: too see any meaningful risk of race condition occuring, you probably need each of the goroutines to increment the counter 1000 times in a loop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really to trigger a data race bug with go test -race.
If we disable locking, this reliably produces the race:

go test -race
==================
WARNING: DATA RACE
Read at 0x00c000156240 by goroutine 9:
  github.com/kong/deck/solver.(*AtomicInt32Counter).Increment()
      /home/hbagdi/deck/solver/solver.go:31 +0x6f
  github.com/kong/deck/solver.TestStats_CreateOps.func1()
      /home/hbagdi/deck/solver/solver_test.go:18 +0x66

Previous write at 0x00c000156240 by goroutine 8:
  github.com/kong/deck/solver.(*AtomicInt32Counter).Increment()
      /home/hbagdi/deck/solver/solver.go:31 +0x84
  github.com/kong/deck/solver.TestStats_CreateOps.func1()
      /home/hbagdi/deck/solver/solver_test.go:18 +0x66

Goroutine 9 (running) created at:
  github.com/kong/deck/solver.TestStats_CreateOps()
      /home/hbagdi/deck/solver/solver_test.go:16 +0xf2
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1194 +0x202

Goroutine 8 (finished) created at:
  github.com/kong/deck/solver.TestStats_CreateOps()
      /home/hbagdi/deck/solver/solver_test.go:16 +0xf2
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1194 +0x202
==================
--- FAIL: TestStats_CreateOps (0.00s)
    testing.go:1093: race detected during execution of test
FAIL
exit status 1
FAIL    github.com/kong/deck/solver     0.008s

@hbagdi hbagdi merged commit 3423945 into main May 21, 2021
@hbagdi hbagdi deleted the fix/data-race branch May 21, 2021 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants